package com.it.you.base.bingfa.jikeshijian;

import java.util.concurrent.TimeUnit;

/**
 * @Author : YouHui
 * @create 2023/5/31 20:25
 */
public class ThreadTest02 {
    public static void main(String[] args) throws InterruptedException {
        //new ThreadLocal<>() 子线程不可以获取到内容
        ThreadLocal<Integer> ttlAge = new ThreadLocal<>();
        ttlAge.set(30);
        //new InheritableThreadLocal<>() 创建的线程共享数据，子线程可以获取
        ThreadLocal<String> ttlName = new InheritableThreadLocal<>();
        ttlName.set("youhui");
        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "获得线程Age变量：" + ttlAge.get());
            System.out.println(Thread.currentThread().getName() + "获得线程Name变量：" + ttlName.get());
            System.out.println(Thread.currentThread().getName() + "start working");
        }).start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println(Thread.currentThread().getName() + "获得线程Age变量：" + ttlAge.get());
        System.out.println(Thread.currentThread().getName() + "获得线程Name变量：" + ttlName.get());
    }
}
